package biz.inspeed.test.mina.m2;

import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.commons.lang3.StringUtils;
import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.core.session.IoSessionConfig;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MinaClient {
	private static final Logger log = LoggerFactory.getLogger("socket");

	private NioSocketConnector connector;;
	private SocketAddress address = null; // new InetSocketAddress
											// ( ServerConstans.HOST
											// ,ServerConstans.PORT
											// );
	private long interval = 3000; // TimeUnit.MILLISECONDS.convert
									// (
									// 3
									// ,
									// TimeUnit.SECONDS
									// );
	private static MinaClient client = null;
	String host = "192.168.0.11";

	int port = 1000;
	private MinaClient() {
		
		address = new InetSocketAddress(host, port);
		connector = new NioSocketConnector();
		connector.setDefaultRemoteAddress(address);
		DefaultIoFilterChainBuilder chain = connector.getFilterChain();
		ProtocolCodecFilter filter = new ProtocolCodecFilter(
				new ObjectSerializationCodecFactory());
		chain.addLast("objectFilter", filter);
		connector.setHandler(new MinaIoHandler());
		Timer t = new Timer();
		t.schedule(new ConnectTask(), 0, interval);
	}

	private class ConnectTask extends TimerTask {

		@Override
		public void run() {
			log.debug("ConnectTask---自动从新连接中。。。。");
			connector.setConnectTimeoutMillis(5);
			connector.setDefaultRemoteAddress(new InetSocketAddress("", 1000));
			log.info("MinaClient.ConnectTask.run().."	+host + ": " + port);
			ConnectFuture cf = connector.connect();
			cf.awaitUninterruptibly();
			// cf.addListener ( ConnectorListener.getInstance () );
			log.debug(StringUtils.repeat("-C-", 20));
			try {
				IoSession session = cf.getSession();
				
				log.debug(StringUtils.repeat("_ON_", 20));
				IoSessionConfig config = session.getConfig();// .setThroughputCalculationInterval
																// ( 1 );
				log.debug(config.getClass().getCanonicalName());
				config.setThroughputCalculationInterval(0);
				config.setWriteTimeout(10);
				
				session.getCloseFuture().awaitUninterruptibly();
			} catch (Exception ex) {
				log.debug(StringUtils.repeat("_OFF_", 20));
				log.error("--ConnectTask--", ex);
			}
			// if ( CommonUtil.online ( "MinaClient$ConnectTask" ) )
			// {
			// CloseFuture closeFuture = cf.getSession ().getCloseFuture ();
			// closeFuture.addListener ( CloseListener.getInstance () );
			// closeFuture.awaitUninterruptibly ();
			// }
			log.debug("ConnectTask---连接失败了。。。");
		}
	}

	/**
	 * 开始连接
	 */
	// static NioSocketConnector connector = new NioSocketConnector ();

	public static void startConnect() {
		if (client == null) {
			client = new MinaClient();
		} else {
			log.debug("=现在是为了少修改代码= show this means i am lazy");
		}
	}

	public static boolean isOnline() {
		return false;
	}

}
